package com.kfm.dao;

import com.kfm.model.KfmUser;
import com.kfm.util.LoadDatasource;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * 对数据表users的增删改查操作
 */
public class UserDao {
    QueryRunner queryRunner;
    BasicRowProcessor basicRowProcessor;

    public UserDao() throws Exception {
        queryRunner = new QueryRunner(LoadDatasource.getDatasource());
        // 处理数据行并将其映射到 Java Bean 对象上。
        GenerousBeanProcessor generousBeanProcessor = new GenerousBeanProcessor();
        basicRowProcessor = new BasicRowProcessor(generousBeanProcessor);
    }

    public KfmUser selectByUsernameAndPassword(String username, String password) throws SQLException {
        String sql = "select *  from `kfm_user` where username = ? and password = ?";
        BeanHandler<KfmUser> kfmUserBeanHandler = new BeanHandler<>(KfmUser.class, basicRowProcessor);
        return queryRunner.query(sql, kfmUserBeanHandler, username, password);
    }

}
